﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CommHelper
{
    public class DESEncryptHelper
    {
        private static string _keyValue = AppSettingsHelper.GetText("deskey");
        private static string _defKey = "4E98F6E2-9C91-417a-897B-E0E7971F63F7";

        public DESEncryptHelper()
        {
        }

        #region ========加密========

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Encrypt(string Text)
        {
            if (string.Empty == _keyValue)
            {
                _keyValue = _defKey;
            }

            return Encrypt(Text, _keyValue);
        }

        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = Encoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = Encoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return ConvertHelper.BytesToString(ms.ToArray());
        }

        #endregion ========加密========

        #region ========解密========

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string Decrypt(string Text)
        {
            if (string.Empty == _keyValue)
            {
                _keyValue = _defKey;
            }

            return Decrypt(Text, _keyValue);
        }

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public static string Decrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = ConvertHelper.StringToBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        #endregion ========解密========

        #region MD5加密

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string EncryptMD5(string Text)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Text, "md5");
        }

        #endregion MD5加密

        #region URL Encode

        public static string UrlEncode(string uri)
        {
            string uriEncode = System.Web.HttpUtility.UrlEncode(uri, Encoding.UTF8);
            uriEncode = uriEncode.Replace("+", "%20");

            return uriEncode;
        }

        #endregion URL Encode
    }
}